/*
 * @(#)BaseService.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */

package com.bis.framework.base.service;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.bis.common.dao.CommonDao;
import com.bis.framework.base.entity.BaseEntity;
import com.bis.framework.exception.SystemException;
import com.bis.framework.util.common.COConstants;
import com.bis.framework.util.common.CommonSqlUtil;
import com.bis.framework.util.common.MessagesUtil;
import com.bis.framework.util.common.Page;
import com.bis.framework.util.common.PageData;

/**
 * Base Service
 * 
 * @author BIS 2012-12-24
 */
public abstract class BaseService implements Service
{
    /**
     * Logger
     */
    protected Logger    logger    = Logger.getLogger(getClass());
    /**
     * Common DAO
     */
    protected CommonDao commonDao = null;

    /**
     * @return the commonDao
     */
    public CommonDao getCommonDao()
    {
        return commonDao;
    }

    /**
     * @param commonDao the commonDao to set
     */
    public void setCommonDao(CommonDao commonDao)
    {
        this.commonDao = commonDao;
    }

    /**
     * 1.保存实体类数据。
     */
    public void saveObj(BaseEntity objBaseEntity)
    {
        try
        {
            commonDao.saveObj(objBaseEntity);
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

    /**
     * 2.保存实体类列表数据。
     * @Description:
     * @param lstObj
     */
    public void saveObj(List objList)
    {
        try
        {
            commonDao.saveObj(objList);
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

    /**
     * 3.更新实体类数据。
     */
    public void updateObj(BaseEntity obj, CommonSqlUtil commonSqlUtil)
    {
        try
        {
            commonDao.updateObj(commonSqlUtil.buildUpdateSql(obj));
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

    /**
     * 4.更新实体类全字段。
     */
    public void updateObjAllPropertiesById(BaseEntity sourceObj)
    {
        try
        {
            commonDao.updateObj(CommonSqlUtil.buildUpdateSqlForAllProperties(sourceObj));
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

    /**
     * 5.根据实体类主键进行数据删除。
     */
    public void deleteObjById(BaseEntity sourceObj)
    {
        try
        {
            commonDao.deleteObjById(CommonSqlUtil.buildDeleteSql(sourceObj));
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

    /**
     * 6.根据实体类数据列表主键进行数据删除。
     */
    public void deleteObjById(final List<BaseEntity> lstObj)
    {
        try
        {
            commonDao.deleteObjById(lstObj);
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

    /**
     * 7.查询数据分页信息。
     */
    public PageData queryPagingData(String nameSpace, Map map, Page page)
    {
        try
        {
            commonDao.setNameSpace(nameSpace);
            PageData pageData = commonDao.selectByMap(map, page);
            return pageData;
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

    /**
     * 8.根据给定的实体类进行数据查询。
     */
    public List selectByEntity(String nameSpace, BaseEntity sourceObj)
    {
        try
        {
            commonDao.setNameSpace(nameSpace);
            return commonDao.selectByEntity(sourceObj);
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

    /**
     * 9.根据主键进行数据查询。
     */
    public Object selectByPrimaryKey(String nameSpace, Object id)
    {
        try
        {
            commonDao.setNameSpace(nameSpace);
            return commonDao.selectByPrimaryKey(id);
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

    /**
     * 10.根据主键进行数据删除。
     */
    public void deleteByPrimaryKey(String nameSpace, String key)
    {
        try
        {
            commonDao.setNameSpace(nameSpace);
            commonDao.deleteByPrimaryKey(key);
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

    /**
     * 11.根据主键列表进行数据删除。
     */
    public void deleteByPrimaryKey(String nameSpace, List keys)
    {
        try
        {
            commonDao.setNameSpace(nameSpace);
            commonDao.deleteByPrimaryKey(keys);
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

    /**
     * 12.对数据记录Count进行验证。
     */
    public Integer checkNameDuplication(Class targetClass, String inputValue, String originalityValue)
    {
        try
        {
            return commonDao.checkNameDuplication(CommonSqlUtil.buildCheckNameDuplicationSql(targetClass, inputValue, originalityValue));
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

    /**
     * 13.根据条件列表用存储过程进行数据分页查询。
     */
    public PageData queryPagingDataByProc(String nameSpaceAndId, Map mapCondition, Page page)
    {
        try
        {
            commonDao.setNameSpaceAndId(nameSpaceAndId);

            mapCondition.put("pageSize", String.valueOf(COConstants.PAGE_COUNT));
            mapCondition.put("pageIndex", String.valueOf(page.getPage()));
            String ordertype = (String) mapCondition.get("orderType");
            if (null == ordertype || "".equals(ordertype))
            {
                mapCondition.put("OrderType", "0");
            }
            else
            {
                mapCondition.put("OrderType", "1");
            }

            return commonDao.selectByProc(page, mapCondition);
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

    /**
     * 14.根据条件列表用存储过程进行数据查询。
     */
    public List queryDataByProc(String nameSpaceAndId, Map mapCondition)
    {
        try
        {
            commonDao.setNameSpaceAndId(nameSpaceAndId);
            return commonDao.selectByProc(mapCondition);
        }
        catch (Exception e)
        {
            logger.error("异常信息:", e);
            throw new SystemException(MessagesUtil.getMessage("system001", "default", null));
        }
    }

}
